package com.tech.algorithm.stack;

/**
 * @Description: 采用数组实现一个栈
 * @Author: zl
 * @Date: 2019/8/15 22:54
 */
public class ArrayStackDemo {


    int top = -1;//表示栈顶
    int[] arr;
    int maxSize = 0;

    ArrayStackDemo(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    // 栈满
    boolean isFull() {
        return top == maxSize - 1;
    }

    boolean isEmpty() {
        return top == -1;
    }


    void push(int x) {
        if (isFull()) {
            System.out.println("stack is full");
            return;
        }
        top++;
        arr[top] = x;
    }

    int pop() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[top--];
    }

    void show() {
        for (int i = arr.length - 1; i >= 0; i--) {
            System.out.printf("%d\n", arr[i]);
        }

    }


    public static void main(String[] args) {
        ArrayStackDemo demo = new ArrayStackDemo(5);
        demo.push(1);
        demo.push(2);
        demo.push(3);
        demo.show();
        System.out.println(demo.pop());
    }


}
